<template>
  <div class="app-container">

    <el-row style="margin-bottom: 10px;">
      <el-button @click="submit" class="pan-btn success-btn" icon="el-icon-s-promotion" style="float: right;" type="success" size="mini">
        {{ isUpdate ? '重新发送' : isReregist ? ('发送至' + fromUserName) : '发送' }}
      </el-button>

      <el-tooltip effect="dark" :content="isFullLast ? '不填充' : '自动填充'" placement="top">
        <el-switch
          :disabled="fullLastDisabled"
          style="float: right;
          margin-right: 40px;
          margin-top: 8px;"
          v-model="isFullLast"
          @change="changeFullLast"
          active-color="#7cbd36"
          inactive-color="#ff4949"
          inactive-text="上一单">
        </el-switch>
      </el-tooltip>
    </el-row>

    <el-row :gutter="12">
      <!-- 基本信息头 -->
      <el-col :span="12">
        <el-card shadow="hover" class="card">
          <h4><b>基本信息头</b></h4>
          <el-form ref="dataForm" :model="dataForm" label-width="80px">
            <el-form-item label="物料名称:" prop="productName">
              <el-form :model="product" v-if="product != ''" :inline="true">
                <el-form-item>
                  {{product.productName}}
                </el-form-item>
                <el-form-item label="物料代码:">
                  {{product.productGmp}}
                </el-form-item>
                <el-form-item label="货品规格:">
                  <el-input id="productSpec" v-model="product.productSpec" placeholder="货品规格" size="small" />
                </el-form-item>
                <el-form-item>
                  <el-button
                    :disabled="isReregist || isUpdate"
                    type="info"
                    class="pan-btn info-btn"
                    @click="showProductSelect"
                    size="mini">
                    重新选择
                  </el-button>
                </el-form-item>
              </el-form>
              <el-button
                v-if="product == ''"
                type="info"
                class="pan-btn info-btn"
                @click="showProductSelect"
                size="mini">
                请选择
              </el-button>
            </el-form-item>

            <el-form-item label="原厂批号:" prop="originalBatch">
              <el-input id="originalBatch" v-model="yfbList.originalBatch" @change="yfbList.productionBatch = yfbList.originalBatch" placeholder="原厂批号" size="small" />
            </el-form-item>

            <el-form-item label="入厂批号:" prop="batchNum">
              <el-input id="batchNum" v-model="checkList.batchNumPrefix" @change="setBatchNum" placeholder="入厂批号" size="small" style="width: 100%;" />
            </el-form-item>

            <el-form-item label="包装规格:" prop="productPackSpec">
              <el-autocomplete
                id="productPackSpec"
                v-model="checkList.productPackSpec"
                @change="setProductPackSpec"
                :fetch-suggestions="queryProductPackSpec"
                placeholder="包装规格"
                size="small"
                style="width: 100%;" />
            </el-form-item>
            <el-form-item label="接收数量:">
              <el-input id="pleaseCheckQuantity" v-model="yfbList.pleaseCheckQuantity" @change="setpleaseCheckQuantity" placeholder="接收数量(数值)" size="small">
               <el-select id="grossUnitId" @change="setGrossUnit" value-key="unitId" v-model="dataForm.grossUnit" slot="append" placeholder="单位" style="width: 100px;">
                 <el-option v-for="item in unitList" :key="item.unitId" :label="item.unitName" :value="item" />
                </el-select>
              </el-input>
            </el-form-item>

            <el-form-item prop="grossQuantity">
              <span slot="label">请验总量:</span>
              <el-input id="grossQuantity" v-model="yfbList.grossQuantity" @change="setGrossQuantity" placeholder="请验总量(数值)" size="small">
                <el-select id="grossUnitId" @change="setGrossUnit" value-key="unitId" v-model="dataForm.grossUnit" slot="append" placeholder="单位" style="width: 100px;">
                  <el-option v-for="item in unitList" :key="item.unitId" :label="item.unitName" :value="item" />
                </el-select>
              </el-input>
            </el-form-item>
            <el-form-item>
              <span slot="label">接收件数:</span>
              <el-input id="pleaseCheckPiece" v-model="yfbList.pleaseCheckPiece" @change="setPleaseCheckPiece" placeholder="件数(数值)" size="small">
                <template slot="append"><div style="width: 59px;">件</div></template>
              </el-input>
            </el-form-item>

            <el-form-item prop="grossPiece">
              <span slot="label">请验件数:</span>
              <el-input id="grossPiece" v-model="yfbList.grossPiece" @change="setGrossPiece" placeholder="请验件数(数值)" size="small">
                <template slot="append"><div style="width: 59px;">件</div></template>
              </el-input>
            </el-form-item>

            <!-- <el-form-item prop="grossQuantity">
              <span slot="label">总&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;量:</span>
              <el-input id="grossQuantity" v-model="yfbList.grossQuantity" @change="setGrossQuantity" placeholder="总量(数值)" size="small">
                <el-select id="grossUnitId" @change="setGrossUnit" value-key="unitId" v-model="dataForm.grossUnit" slot="append" placeholder="单位" style="width: 100px;">
                  <el-option v-for="item in unitList" :key="item.unitId" :label="item.unitName" :value="item" />
                </el-select>
              </el-input>
            </el-form-item>

            <el-form-item prop="grossPiece">
              <span slot="label">件&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;数:</span>
              <el-input id="grossPiece" v-model="yfbList.grossPiece" @change="setGrossPiece" placeholder="件数(数值)" size="small">
                <template slot="append"><div style="width: 59px;">件</div></template>
              </el-input>
            </el-form-item> -->

            <el-form-item label="包装容器:" prop="packContainerId">
              <el-radio-group v-model="dataForm.packContainerId" @change="setPackContainer" size="small">
                <el-radio-button v-for="item in packContainerList" :key="item.packContainerId" :label="item.packContainerId">{{item.packContainerName}}</el-radio-button>
              </el-radio-group>
            </el-form-item>

            <el-form-item label="请验目的:" prop="checkPurposeId">
              <el-radio-group v-model="dataForm.checkPurposeId" @change="setCheckPurpose" size="small">
                <el-radio-button v-for="item in checkPurposeList" :key="item.checkPurposeId" :label="item.checkPurposeId">{{item.checkPurposeName}}</el-radio-button>
              </el-radio-group>
            </el-form-item>

          </el-form>
        </el-card>
      </el-col>
      <!-- 基本信息尾 -->
      <el-col :span="12">
        <el-card shadow="hover" class="card">
          <h4><b>基本信息尾</b></h4>
          <el-form ref="dataForm" :model="dataForm" label-width="110px">
            <el-form-item prop="isAllTest" v-if="product != ''">
              <span slot="label">请&nbsp;&nbsp;检&nbsp;&nbsp;项&nbsp;&nbsp;&nbsp;目:</span>
              <div v-if="allItems.length == 0" style="color: #F56C6C;">请添加此货品的检验标准</div>
              <div v-else>
                <el-radio-group v-model="checkList.isAllTest" @change="changeItems" size="small">
                  <el-radio-button :label="0">全检</el-radio-button>
                  <el-radio-button :label="1">其他</el-radio-button>
                </el-radio-group>
                <el-button
                  style="margin-left: 20px;"
                  type="info"
                  class="pan-btn info-btn"
                  @click="showStandardDrawer"
                  size="mini">
                  查看项目
                </el-button>
              </div>
            </el-form-item>

            <el-form-item prop="firstVerdict">
              <span slot="label">初&nbsp;&nbsp;检&nbsp;&nbsp;结&nbsp;&nbsp;&nbsp;论:</span>
              <el-radio-group v-model="yfbList.firstVerdict" size="small">
                <el-radio-button :label="0">符合规定</el-radio-button>
                <el-radio-button :label="1">不符合规定</el-radio-button>
              </el-radio-group>
            </el-form-item>

            <el-form-item label="物料进厂日期:" prop="comeDate">
              <el-date-picker
                id="comeDate"
                v-model="yfbList.comeDate"
                align="right"
                type="date"
                placeholder="选择日期"
                :picker-options="pickerOptions"
                style="width: 100%;"
                format="yyyy 年 MM 月 dd 日"
                value-format="yyyy-MM-dd HH:mm:ss"
                size="small">
              </el-date-picker>
            </el-form-item>

            <el-form-item prop="comeUserId">
              <span slot="label">物&nbsp;料&nbsp;接&nbsp;收人:</span>
              <el-select id="comeUserId" v-model="dataForm.comeUser" value-key="userId" @change="setComeUser" filterable placeholder="物料接收人" style="width: 100%;" size="small">
                <el-option
                  v-for="item in userList"
                  :key="item.userId"
                  :label="item.name + '(' + item.username + ')'"
                  :value="item">
                </el-option>
              </el-select>
            </el-form-item>

            <el-form-item prop="registCheckDepartmentId">
              <span slot="label">请&nbsp;&nbsp;验&nbsp;&nbsp;部&nbsp;&nbsp;&nbsp;门:</span>
              {{checkList.registCheckDepartmentName}}
            </el-form-item>

            <el-form-item prop="registCheckUserId">
              <span slot="label">请&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;验&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;人:</span>
              {{checkList.registCheckUserName}}
            </el-form-item>

            <el-form-item prop="registCheckDate">
              <span slot="label">请&nbsp;&nbsp;验&nbsp;&nbsp;日&nbsp;&nbsp;&nbsp;期:</span>
              <el-date-picker
                id="registCheckDate"
                v-model="checkList.registCheckDate"
                align="right"
                type="date"
                placeholder="选择日期"
                :picker-options="pickerOptions"
                style="width: 100%;"
                format="yyyy 年 MM 月 dd 日"
                value-format="yyyy-MM-dd HH:mm:ss"
                size="small">
              </el-date-picker>
            </el-form-item>

            <el-form-item prop="remark">
              <span slot="label">备&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;注:</span>
              <el-input v-model="checkList.remark" placeholder="备注" size="small"/>
            </el-form-item>

          </el-form>
        </el-card>
      </el-col>
    </el-row>

    <el-row :gutter="12" style="margin-top: 20px;">
      <!-- 供应商 -->
      <el-col :span="12">
        <el-card shadow="hover" class="card">
          <h4><b>供应商</b></h4>
          <el-form ref="dataForm" :model="dataForm" label-width="140px">
            <el-form-item prop="supplierId">
              <span slot="label">供&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;应&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;商:</span>
              <el-select
                id="supplierId"
                v-model="dataForm.supplier"
                value-key="supplierId"
                @change="setSupplier"
                filterable
                remote
                reserve-keyword
                :remote-method="searchSupplier"
                placeholder="关键词搜索"
                :loading="supplierLoading"
                style="width: 100%;"
                size="small">
                <el-option
                  v-for="item in supplierList"
                  :key="item.supplierId"
                  :label="item.supplierName"
                  :value="item">
                </el-option>
              </el-select>
            </el-form-item>

            <el-form-item prop="supplierLevel">
              <span slot="label">供&nbsp;&nbsp;&nbsp;应&nbsp;&nbsp;&nbsp;商&nbsp;&nbsp;&nbsp;等&nbsp;&nbsp;&nbsp;级:</span>
              <el-radio-group v-model="yfbList.supplierLevel" size="small">
                <el-radio-button label="合格供应商" value="合格供应商" />
                <el-radio-button label="新增供应商" value="新增供应商" />
              </el-radio-group>
            </el-form-item>

            <el-form-item label="批准文号/注册证号:" prop="approvalNum">
              <el-input id="approvalNum" v-model="yfbList.approvalNum" placeholder="批准文号/注册证号" size="small" />
            </el-form-item>

            <el-form-item>
              <el-select slot="label" v-model="select" @change="changeSelect" size="small">
                <el-option label="有效期" value="validityDate">有效期</el-option>
                <el-option label="复验期" value="reinspectDate">复验期</el-option>
              </el-select>
              <el-date-picker
                v-if="selectDateUnit == 0"
                id="selectDate"
                v-model="selectDate"
                type="date"
                placeholder="精确到日"
                style="width: 93%;"
                format="yyyyMMdd"
                value-format="yyyy-MM-dd HH:mm:ss"
                size="small">
              </el-date-picker>
              <el-date-picker
                v-if="selectDateUnit == 1"
                id="selectDate"
                v-model="selectDate"
                type="month"
                placeholder="精确到月"
                style="width: 93%;"
                format="yyyyMM"
                value-format="yyyy-MM-dd HH:mm:ss"
                size="small">
              </el-date-picker>
              <el-tooltip :content="selectDateUnitName" placement="top">
                <el-switch
                  v-model="selectDateUnitName"
                  active-color="#13ce66"
                  inactive-color="#ff4949"
                  @click.native="changeSelectDateUnit()"
                  active-value="日"
                  inactive-value="月"
                  size="mini">
                </el-switch>
              </el-tooltip>
            </el-form-item>

            <el-form-item label="随货文件:" prop="followFileIds">
              <span slot="label">随&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;货&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;文&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;件:</span>
              <el-checkbox-group v-model="followFileIds" @change="yfbList.followFileIds = followFileIds.join(',')" size="small">
                <el-checkbox-button v-for="item in followFileList" :label="item.followFileId" :key="item.followFileId">{{item.followFileName}}</el-checkbox-button>
              </el-checkbox-group>
            </el-form-item>

          </el-form>
        </el-card>
      </el-col>
      <!-- 生产企业 -->
      <el-col :span="12">
        <el-card shadow="hover" class="card">
          <h4><b>生产企业</b></h4>
          <el-form ref="dataForm" :model="dataForm" label-width="80px">

            <el-form-item label="生产日期:" prop="productionDate">
              <el-date-picker
                v-if="productionDateUnit == 0"
                id="productionDate"
                v-model="yfbList.productionDate"
                align="right"
                type="date"
                placeholder="精确到日"
                :picker-options="pickerOptions"
                style="width: 93%;"
                format="yyyyMMdd"
                value-format="yyyy-MM-dd HH:mm:ss"
                size="small">
              </el-date-picker>
              <el-date-picker
                v-if="productionDateUnit == 1"
                id="productionDate"
                v-model="yfbList.productionDate"
                align="right"
                type="month"
                placeholder="精确到月"
                style="width: 93%;"
                format="yyyyMM"
                value-format="yyyy-MM-dd HH:mm:ss"
                size="small">
              </el-date-picker>
              <el-tooltip :content="productionDateUnitName" placement="top">
                <el-switch
                  v-model="productionDateUnitName"
                  active-color="#13ce66"
                  inactive-color="#ff4949"
                  @click.native="changeProductionDateUnit()"
                  active-value="日"
                  inactive-value="月"
                  size="mini">
                </el-switch>
              </el-tooltip>
            </el-form-item>

            <el-form-item label="生产企业:" prop="manufacturerId">
              <el-select
                id="manufacturerId"
                v-model="dataForm.manufacturer"
                value-key="manufacturerId"
                @change="setManufacturer"
                filterable
                remote
                reserve-keyword
                :remote-method="searchManufacturer"
                placeholder="关键词搜索"
                :loading="manufacturerLoading"
                style="width: 100%;"
                size="small">
                <el-option
                  v-for="item in manufacturerList"
                  :key="item.manufacturerId"
                  :label="item.manufacturerName"
                  :value="item">
                </el-option>
              </el-select>
            </el-form-item>

            <el-form-item label="生产批号:" prop="productionBatch">
              <el-input id="productionBatch" v-model="yfbList.productionBatch" placeholder="生产批号" size="small" />
            </el-form-item>

            <el-form-item label="包装状况:" prop="packStatusIds">
              <el-checkbox-group v-model="packStatusIds" @change="yfbList.packStatusIds = packStatusIds.join(',')" size="small">
                <el-checkbox-button v-for="item in packStatusList" :label="item.packStatusId" :key="item.packStatusId">{{item.packStatusName}}</el-checkbox-button>
              </el-checkbox-group>
            </el-form-item>

            <el-form-item label="包装异常:" prop="packExceptionIds">
              <el-checkbox-group v-model="packExceptionIds" @change="yfbList.packExceptionIds = packExceptionIds.join(',')" size="small">
                <el-checkbox-button v-for="item in packExceptionList" :label="item.packExceptionId" :key="item.packExceptionId">{{item.packExceptionName}}</el-checkbox-button>
              </el-checkbox-group>
            </el-form-item>

          </el-form>
        </el-card>
      </el-col>
    </el-row>

    <product-select
      @getProduct="getProduct"
      @showProductSelect="showProductSelect"
      :childDataForm="childDataForm"
      v-if="productSelectVisible" />

    <standard-drawer
      ref="standard"
      :childDataForm="childDataForm"
      @close="closeStandardDrawer"
      @getStandard="getStandard"
      v-if="standardDrawerVisible" />

  </div>
</template>

<script>
  import { mapGetters } from 'vuex'
  import { CODE_OK } from '@/common/js/config'
  import { isBlank,parseTime } from '@/common/js/utils'
  import { isNumber } from '@/common/js/validate'
  import ProductSelect from '@/views/modules/base/product-select'
  import StandardDrawer from '@/views/modules/base/standard-drawer'
  import {
    getUnitList,
    getPackContainerList,
    getCheckPurposeList,
    getFollowFileList,
    getUserList,
    getPackStatusList,
    getPackExceptionList,
    registCheck,
    getRegistCheckInfo,
    getLastCheckForRegist,
    getProductPackSpecByProductId
  } from '@/api/check'
  import { getSupplierList } from "@/api/supplier"
  import { getManufacturerList } from "@/api/manufacturer"
  import { getStandardByProductId } from '@/api/standard'
  export default {
    name: 'registcheck202',
    components: {
    	ProductSelect,StandardDrawer
    },
    data() {
      return {
        printCheckVersion: '02',
        fullLastDisabled: true,
        isFullLast: false,
        //被退回标志
        isReregist: false,
        //发起后未取样修改标志
        isUpdate: false,
        fromUserName: null,
        fromStatus: null,
        checkReturnId: null,
        unitList: [],
        packContainerList: [],
        checkPurposeList: [],
        followFileList: [],
        userList: [],
        supplierLoading: false,
        supplierList: [],
        manufacturerLoading: false,
        manufacturerList: [],
        packStatusList: [],
        packExceptionList: [],
        standard: '',
        allItems: [],
        items: [],
        followFileIds: [],
        packStatusIds: [],
        packExceptionIds: [],
        productSelectVisible: false,
        standardDrawerVisible: false,
        checkList: {
          checkTypeId: 2,
          checkTypeName: '辅料',
          registCheckDepartmentId: '',
          registCheckDepartmentName: '',
          registCheckUserId: '',
          registCheckUserName: '',
          isTest: 0,
          isAllTest: 1,
          status: 1,
          productId: '',
          productName: '',
          productGmp: '',
          productSpec: '',
          productPackSpec: '',
          registCheckDate: ''
        },
        yfbList: {
          followFileIds: '',
          packStatusIds: '',
          packExceptionIds: '',
          productionBatch: '',
          validityDate: '',
          reinspectDate: '',
          comeUserId: '',
          comeUserName: ''
        },
        product: '',
        dataForm: {
          grossUnit: '',
          packContainerId: '',
          checkPurposeId: '',
          comeUser: '',
          supplier: '',
          manufacturer: ''
        },
        select: 'validityDate',
        selectDateUnit: 1,
        selectDateUnitName: '月',
        productionDateUnit: 0,
        productionDateUnitName: '日',
        selectDate: '',
        childDataForm: '',
        productPackSpecs: [],
        queryedProductPackSpecs: false
      }
    },
    created() {
      if (!isBlank(this.$route.query.checkId)) {
        this.isReregist = true
      }
      this.init()
      this.getYfbInfo()
    },
    computed: {
      ...mapGetters([
         'userInfo'
      ])
    },
    methods: {
      init() {
        if (this.isReregist) {
          const params = {
            checkId: this.$route.query.checkId,
            content: 'yfb'
          }
          getRegistCheckInfo(params).then(res => {
            if (res.code === CODE_OK) {
              this.fillData(res.info)
              this.setReregistInfo()
            }else{
              this.$message({
                message: res.msg,
                type: 'error',
                duration: 1000,
                onClose: () => {
                  window.history.go(-1)
                }
              })
            }
          })
        }else{
          this.setRegistInfo()
        }
      },
      setRegistInfo() {
        this.dataForm.comeUser = {
          userId: this.userInfo.userId,
          name: this.userInfo.name
        }
        this.setComeUser()
        this.checkList.registCheckDepartmentId = this.userInfo.departmentId
        this.checkList.registCheckDepartmentName = this.userInfo.departmentName
        this.checkList.registCheckUserId = this.userInfo.userId
        this.checkList.registCheckUserName = this.userInfo.name
      },
      fillData(info) {
        let checkList = info.checkList
        let yfbList = info.yfbList
        this.checkList = JSON.parse(JSON.stringify(checkList))
        this.yfbList = JSON.parse(JSON.stringify(yfbList))
        let val = isBlank(yfbList.validityDate) ? 'reinspectDate' : 'validityDate'
        this.selectDate = this.yfbList[val]
        this.select = val
        this.dataForm.grossUnit = {
          unitId: yfbList.grossUnitId,
          unitName: yfbList.grossUnitName
        }
        this.dataForm.packContainerId = yfbList.packContainerId
        this.dataForm.checkPurposeId = yfbList.checkPurposeId
        this.dataForm.comeUser = {
          userId: yfbList.comeUserId,
          name: yfbList.comeUserName
        }
        this.dataForm.supplier = {
          supplierId: yfbList.supplierId,
          supplierName: yfbList.supplierName
        }
        this.dataForm.manufacturer = {
          manufacturerId: yfbList.manufacturerId,
          manufacturerName: yfbList.manufacturerName
        }
        this.searchSupplier(this.dataForm.supplier.supplierName)
        this.searchManufacturer(this.dataForm.manufacturer.manufacturerName)
        this.followFileIds = yfbList.followFileIds.split(",").map(value => {
          return parseInt(value)
        })
        this.packStatusIds = yfbList.packStatusIds.split(",").map(value => {
          return parseInt(value)
        })
        if (!isBlank(yfbList.packExceptionIds)) {
          this.packExceptionIds = yfbList.packExceptionIds.split(",").map(value => {
            return parseInt(value)
          })
        }
        if (this.isReregist) {
          this.getProduct(info.product)
          this.standard = info.standard
        }
        if (!isBlank(checkList.productSpec)) {
          this.product.productSpec = checkList.productSpec
        }
        if (this.isFullLast && this.standard.insideInspectionStandardId != info.standard.insideInspectionStandardId) {
          if (checkList.isAllTest == 0) {
            this.checkList.isAllTest = checkList.isAllTest
            this.items = JSON.parse(JSON.stringify(this.allItems))
          }else{
            this.$message.error("标准已升级，请自己选择检验项目")
          }
        }else{
          this.checkList.isAllTest = checkList.isAllTest
          this.items = info.items
        }
      },
      setReregistInfo() {
        if (this.$route.query.checkReturnId != 0) {
          this.fromUserName = this.$route.query.fromUserName
          this.fromStatus = this.$route.query.fromStatus
          this.checkReturnId = this.$route.query.checkReturnId
        }else{
          this.isUpdate = true
        }
      },
      getYfbInfo() {
        getUnitList().then(res => {
          if (res.code === CODE_OK) {
            this.unitList = res.list
          }else{
            this.$message.error(res.msg)
          }
        })
        const params = {
          checkTypeId: 1,
          printVersion: this.printCheckVersion
        }
        getPackContainerList(params).then(res => {
          if (res.code === CODE_OK) {
            this.packContainerList = res.list
          }else{
            this.$message.error(res.msg)
          }
        })
        getCheckPurposeList(params).then(res => {
          if (res.code === CODE_OK) {
            this.checkPurposeList = res.list
          }else{
            this.$message.error(res.msg)
          }
        })
        getFollowFileList(params).then(res => {
          if (res.code === CODE_OK) {
            this.followFileList = res.list
          }else{
            this.$message.error(res.msg)
          }
        })
        getUserList({
          departmentId: this.userInfo.departmentId
        }).then(res => {
          if (res.code === CODE_OK) {
            this.userList = res.list
          }else{
            this.$message.error(res.msg)
          }
        })
        getPackStatusList(params).then(res => {
          if (res.code === CODE_OK) {
            this.packStatusList = res.list
          }else{
            this.$message.error(res.msg)
          }
        })
        getPackExceptionList(params).then(res => {
          if (res.code === CODE_OK) {
            this.packExceptionList = res.list
          }else{
            this.$message.error(res.msg)
          }
        })
      },
      submit() {
        let val = this.select
        this.yfbList[val] = this.selectDate
        this.yfbList.selectDateUnit = this.selectDateUnit
        this.yfbList.productionDateUnit = this.productionDateUnit
        this.checkList.isMerge = 1
        this.checkList.printCheckVersion = this.printCheckVersion
        if (this.formVerify()) {
          const data = {
            content: 'yfb',
            fromStatus: this.fromStatus,
            checkReturnId: this.checkReturnId,
            checkList: this.checkList,
            yfbList: this.yfbList,
            product: this.product,
            standard: this.standard,
            items: this.items
          }
          registCheck(data).then(res => {
            if (res.code === CODE_OK) {
              this.$message({
                message: '提交成功',
                type: 'success',
                duration: 1000,
                onClose: () => {
                  window.history.go(-1)
                }
              })
            }else{
              this.$message.error(res.msg)
            }
          })
        }
      },
      changeFullLast() {
        if (this.isFullLast) {
          const params = {
            content: 'yfb',
            productId: this.product.productId
          }
          getLastCheckForRegist(params).then(res => {
            if (res.code === CODE_OK) {
              this.fillData(res.info)
              this.setRegistInfo()
              this.searchSupplier(this.dataForm.supplier.supplierName)
              this.searchManufacturer(this.dataForm.manufacturer.manufacturerName)
            }else{
              this.$message.error(res.msg)
              this.isFullLast = false
            }
          })
        }else{
          //初始化

        }
      },
      changeSelect() {
        this.yfbList.validityDate = null
        this.yfbList.reinspectDate = null
      },
      formVerify() {
        if (isBlank(this.product)) {
          this.$message.error('请选择货品')
          return false
        }
        if (isBlank(this.yfbList.originalBatch)) {
          this.$message.error('请输入原厂批号')
          $("#originalBatch").css({
              "border-color" : "#D08585"
          })
          return false
        }
        $("#originalBatch").removeAttr('style')
        if (isBlank(this.checkList.batchNum)) {
          this.$message.error('请输入入厂批号')
          $("#batchNum").css({
              "border-color" : "#D08585"
          })
          return false
        }
        $("#batchNum").removeAttr('style')
        if (isBlank(this.checkList.productPackSpec)) {
          this.$message.error('请输入包装规格')
          $("#productPackSpec").css({
              "border-color" : "#D08585"
          })
          return false
        }
        $("#productPackSpec").removeAttr('style')
        if (isBlank(this.yfbList.grossQuantity)) {
          this.$message.error('请输入请验数量(数值)')
          $("#grossQuantity").css({
            "border-color": "#D08585"
          })
          return false
        }
        $("#grossQuantity").removeAttr('style')
        if (isBlank(this.yfbList.grossUnitId)) {
          this.$message.error('请选择请验数量(单位)')
          $("#grossUnitId").css({
            "border-color": "#D08585"
          })
          return false
        }
        
        $("#grossUnitId").removeAttr('style')
        if (isBlank(this.yfbList.pleaseCheckQuantity)) {
          this.$message.error('请输入接收数量(数值)')
          $("#pleaseCheckQuantity").css({
            "border-color": "#D08585"
          })
          return false
        }
        
        $("#pleaseCheckQuantity").removeAttr('style')
        if (isBlank(this.yfbList.grossUnitId)) {
          this.$message.error('请选择接收数量(单位)')
          $("#grossUnitIds").css({
            "border-color": "#D08585"
          })
          return false
        }
        $("#grossUnitIds").removeAttr('style')
        if (isBlank(this.yfbList.grossPiece)) {
          this.$message.error('请输入请验件数')
          $("#grossPiece").css({
            "border-color": "#D08585"
          })
          return false
        }
        $("#grossPiece").removeAttr('style')
        if (isBlank(this.yfbList.pleaseCheckPiece)) {
          this.$message.error('请输入件数')
          $("#pleaseCheckPiece").css({
            "border-color": "#D08585"
          })
          return false
        }
        // $("#productPackSpec").removeAttr('style')
        // if (isBlank(this.yfbList.grossQuantity)) {
        //   this.$message.error('请输入总量(数值)')
        //   $("#grossQuantity").css({
        //       "border-color" : "#D08585"
        //   })
        //   return false
        // }
        // $("#grossQuantity").removeAttr('style')
        // if (isBlank(this.yfbList.grossUnitId)) {
        //   this.$message.error('请选择总量(单位)')
        //   $("#grossUnitId").css({
        //       "border-color" : "#D08585"
        //   })
        //   return false
        // }
        // $("#grossUnitId").removeAttr('style')
        // if (isBlank(this.yfbList.grossPiece)) {
        //   this.$message.error('请输入件数')
        //   $("#grossPiece").css({
        //       "border-color" : "#D08585"
        //   })
        //   return false
        // }
        $("#grossPiece").removeAttr('style')
        if (isBlank(this.yfbList.packContainerId)) {
          this.$message.error('请选择包装容器')
          return false
        }
        if (isBlank(this.yfbList.checkPurposeId)) {
          this.$message.error('请选择请检目的')
          return false
        }
        if (this.items.length == 0) {
          this.$message.error('请选择请检项目')
          return false
        }
        if (this.yfbList.firstVerdict != 0 && this.yfbList.firstVerdict != 1) {
          this.$message.error('请选择初检结论')
          return false
        }
        if (isBlank(this.yfbList.comeDate)) {
          this.$message.error('请选择物料进厂日期')
          $("#comeDate").css({
              "border-color" : "#D08585"
          })
          return false
        }
        $("#comeDate").removeAttr('style')
        if (isBlank(this.yfbList.comeUserId)) {
          this.$message.error('请选择物料接收人')
          $("#comeUserId").css({
              "border-color" : "#D08585"
          })
          return false
        }
        $("#comeUserId").removeAttr('style')
        if (isBlank(this.checkList.registCheckDepartmentId) ||isBlank(this.checkList.registCheckUserId)) {
          this.$message.error('登录过期,请刷新页面')
          return false
        }
        if (isBlank(this.checkList.registCheckDate)) {
          this.$message.error('请选择请检日期')
          $("#registCheckDate").css({
              "border-color" : "#D08585"
          })
          return false
        }
        $("#registCheckDate").removeAttr('style')
        if (isBlank(this.yfbList.supplierId)) {
          this.$message.error('请选择供应商')
          $("#supplierId").css({
              "border-color" : "#D08585"
          })
          return false
        }
        $("#supplierId").removeAttr('style')
        if (isBlank(this.yfbList.supplierLevel)) {
          this.$message.error('请选择供应商等级')
          return false
        }
        if (isBlank(this.yfbList.approvalNum)) {
          this.$message.error('请输入批准文号/注册证号')
          $("#approvalNum").css({
              "border-color" : "#D08585"
          })
          return false
        }
        $("#approvalNum").removeAttr('style')
        if (isBlank(this.yfbList.validityDate) && isBlank(this.yfbList.reinspectDate)) {
          this.$message.error('请选择有效期或者复验期')
          $("#selectDate").css({
              "border-color" : "#D08585"
          })
          return false
        }
        $("#selectDate").removeAttr('style')
        if (isBlank(this.yfbList.followFileIds)) {
          this.$message.error('请选择随货文件')
          return false
        }
        // if (isBlank(this.yfbList.productionDate)) {
        //   this.$message.error('请选择生产日期')
        //   $("#productionDate").css({
        //       "border-color" : "#D08585"
        //   })
        //   return false
        // }
        // $("#productionDate").removeAttr('style')
        if (isBlank(this.yfbList.manufacturerId)) {
          this.$message.error('请选择生产企业')
          $("#manufacturerId").css({
              "border-color" : "#D08585"
          })
          return false
        }
        $("#manufacturerId").removeAttr('style')
        if (isBlank(this.yfbList.productionBatch)) {
          this.$message.error('请输入生产批号')
          $("#productionBatch").css({
              "border-color" : "#D08585"
          })
          return false
        }
        $("#productionBatch").removeAttr('style')
        if (isBlank(this.yfbList.packStatusIds)) {
          this.$message.error('请选择包装状况')
          return false
        }
        return true
      },
      changeItems() {
        if (this.checkList.isAllTest == 0) {
          this.items = JSON.parse(JSON.stringify(this.allItems))
        }else{
          this.items = []
          this.showStandardDrawer()
        }
      },
      showProductSelect() {
        this.childDataForm = {
          checkTypeId: this.checkList.checkTypeId
        }
      	this.productSelectVisible = !this.productSelectVisible
      },
      showStandardDrawer() {
        this.childDataForm = {
          product: this.product,
          standard: this.standard,
          allItems: this.allItems,
          checkedItems: this.items
        }
        this.standardDrawerVisible = !this.standardDrawerVisible
      },
      closeStandardDrawer() {
        this.childDataForm = ''
        this.standardDrawerVisible = !this.standardDrawerVisible
      },
      getStandard(standardObj) {
        this.items = standardObj.checkedItems
        this.checkList.isAllTest = standardObj.isAllTest
      },
      getProduct(product) {
        this.product = product
        const params = {
          productId: product.productId
        }
        this.initStandard()
        getStandardByProductId(params).then(res => {
          if (res.code === CODE_OK) {
            this.allItems = res.standard.items
            if (this.allItems.length == 0) return
            if (!this.isReregist) {
              this.standard = {
                insideInspectionStandardId: res.standard.insideInspectionStandardId,
                insideInspectionStandardNum: res.standard.insideInspectionStandardNum,
                nationInspectionStandardNum: res.standard.nationInspectionStandardNum,
                accordingToInside: res.standard.accordingToInside,
                accordingToNation: res.standard.accordingToNation
              }
              this.fullLastDisabled = this.isUpdate ? true : false
            }
          }else{
            this.$message.error(res.msg)
          }
        })
      },
      initStandard() {
        this.standard = ''
        this.allItems = []
        this.items = []
        this.checkList.isAllTest = ''
      },
      queryProductPackSpec(queryString, cb) {
        if (!this.queryedProductPackSpecs && this.product != '') {
          const params = {
            productId: this.product.productId
          }
          getProductPackSpecByProductId(params).then(res => {
            if (res.code === CODE_OK) {
              if (res.list.length != 0) {
                for (let i=0,j=res.list.length; i<j; i++) {
                  this.productPackSpecs.push({
                    value: res.list[i]
                  })
                }
              }
              this.queryedProductPackSpecs = true
            }else{
              this.$message.error(res.msg)
            }
          })
        }else if(this.productPackSpecs.length != 0){
          this.productPackSpecs = this.productPackSpecs.filter(ps => {
            return ps.value.indexOf(queryString) > -1
          })
        }
        cb(this.productPackSpecs)
      },
      //供应商生产企业筛选
      searchSupplier(query) {
        this.supplierLoading = true
        this.supplierList = []
        if (!isBlank(query)) {
          const params = {
          	page: 1,
            limit: 10,
          	supplierName: query
          }
          getSupplierList(params).then(res => {
            if (res.code === CODE_OK) {
              this.supplierList = res.page.list
            }else{
              this.$message.error(res.msg)
            }
            this.supplierLoading = false
          })
        }
      },
      searchManufacturer(query) {
        this.manufacturerLoading = true
        this.manufacturerList = []
        if (!isBlank(query)) {
          const params = {
            page: 1,
            limit: 10,
            manufacturerName: query
          }
          getManufacturerList(params).then(res => {
            if (res.code === CODE_OK) {
              this.manufacturerList = res.page.list
            }else{
              this.$message.error(res.msg)
            }
            this.manufacturerLoading = false
          })
        }
      },
      //根据object赋值id和name
      changeProductionDateUnit() {
        this.productionDateUnit = this.productionDateUnitName == '月' ? 1 : 0
      },
      changeSelectDateUnit() {
        this.selectDateUnit = this.selectDateUnitName == '月' ? 1 : 0
      },
      setBatchNum() {
        this.checkList.batchNum = this.checkList.batchNumPrefix
      },
      setProductPackSpec() {
        this.checkList.productPackSpec = this.checkList.productPackSpec.replace(/KG/g,'kg')
      },
      setGrossQuantity() {
        if (!isNumber(this.yfbList.grossQuantity)) {
          this.$message.error('请输入数值类型')
          this.yfbList.grossQuantity = null
        }
      },
      setGrossUnit() {
        this.yfbList.grossUnitId = this.dataForm.grossUnit.unitId
        this.yfbList.grossUnitName = this.dataForm.grossUnit.unitName
      },
      setGrossPiece() {
        if (!isNumber(this.yfbList.grossPiece)) {
          this.$message.error('请输入数值类型')
          this.yfbList.grossPiece = null
        }
      },
      setPackContainer() {
        let id = this.dataForm.packContainerId
        for (let p of this.packContainerList) {
          if (p.packContainerId == id) {
            this.yfbList.packContainerId = p.packContainerId
            this.yfbList.packContainerName = p.packContainerName
            break
          }
        }
      },
      setCheckPurpose() {
        let id = this.dataForm.checkPurposeId
        for (let c of this.checkPurposeList) {
          if (c.checkPurposeId == id) {
            this.yfbList.checkPurposeId = c.checkPurposeId
            this.yfbList.checkPurposeName = c.checkPurposeName
            break
          }
        }
      },
      setComeUser() {
        this.yfbList.comeUserId = this.dataForm.comeUser.userId
        this.yfbList.comeUserName = this.dataForm.comeUser.name
      },
      setSupplier() {
        this.yfbList.supplierId = this.dataForm.supplier.supplierId
        this.yfbList.supplierName = this.dataForm.supplier.supplierName
      },
      setManufacturer() {
        this.yfbList.manufacturerId = this.dataForm.manufacturer.manufacturerId
        this.yfbList.manufacturerName = this.dataForm.manufacturer.manufacturerName
      }
    }
  }
</script>

<style scoped>
  .el-form-item {
    margin-bottom: 0px !important;
  }
  .el-form-item__content .el-input-group {
    vertical-align: inherit !important;
  }
</style>
